var uploadImage = require('../../utils/uploadFile')

// components/upalod/upload.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    src:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatTime(date) {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      const hour = date.getHours()
      const minute = date.getMinutes()
      const second = date.getSeconds()
      return [year, month, day].map(this.formatNumber).join('-')
    },
    formatNumber(n) {
      n = n.toString()
      return n[1] ? n : '0' + n
    },
    handleUplaod() {
      const _this = this
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          console.log(res)
          var nowTime = _this.formatTime(new Date());
          uploadImage(res.tempFiles[0].tempFilePath, 'cbb/' + nowTime + '/',
            function (result) {
              console.log("======上传成功图片地址为：", result);
              wx.hideLoading();
              _this.setData({
                src:result
              })
              _this.triggerEvent('succ',result)
            },
            function (result) {
              console.log("======上传失败======", result);
              wx.hideLoading()
            }
          )
        },
        fail(err) {
          wx.showToast({
            title: err,
            icon: 'error'
          })
        }
      })
    }
  }
})